export const jumpPage = (url,data) => {
    // 参数处理同 jumpPage
    if (data && typeof data === 'object') {
        const queryParams = Object.keys(data)
            .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
            .join('&');

        if (queryParams) {
            url += (url.includes('?') ? '&' : '?') + queryParams;
        }
    }
    uni.navigateTo({
        url: url
    });
}

export const jumpPageInput = (url,data) => {
    // 参数处理同 jumpPage
    if (data && typeof data === 'object') {
        const queryParams = Object.keys(data)
            .map(key => {
                return `${key}=${data[key]}`;
            })
            .join('&');
        if (queryParams) {
            url += (url.includes('?') ? '&' : '?') + queryParams;
        }
    }
    uni.navigateTo({
        url: url,
        animationType: 'fade-in',
    });
}

export const getUrlParams = () => {
    // #ifdef H5
    // H5 平台特殊处理
    const urlParams = new URLSearchParams(window.location.search);
    const params = {};
    for (let [key, value] of urlParams) {
        params[decodeURIComponent(key)] = decodeURIComponent(value || '');
    }
    return params;
    // #endif

    // #ifndef H5
    try {
        const pages = getCurrentPages();
        if (pages.length === 0) return {};

        const currentPage = pages[pages.length - 1];
        const options = currentPage.options || {};

        const decodedParams = {};
        for (let key in options) {
            if (options.hasOwnProperty(key)) {
                try {
                    decodedParams[decodeURIComponent(key)] = decodeURIComponent(options[key] || '');
                } catch (e) {
                    console.warn('解码参数失败:', key, options[key]);
                    decodedParams[key] = options[key] || ''; // 解码失败时使用原始值
                }
            }
        }
        return decodedParams;
    } catch (e) {
        console.error('获取页面参数失败:', e);
        return {};
    }
    // #endif
}
